//
//  Modifer.swift
//  MasterBoard-Swift
//
//  Created by JunMing on 2022/10/21.
//  
//

import SwiftUI

struct Shadow: ViewModifier {
    var color: Color = Color.black
    func body(content: Content) -> some View {
        content
            .shadow(color: color.opacity(0.2), radius: 3, x: 0, y: 0)
//            .shadow(color: color.opacity(0.2), radius: 5, x: 0, y: 5)
//            .shadow(color: color.opacity(0.2), radius: 1, x: 0, y: 1)
    }
}

// Text允许用户选中
struct EnableSelectModifier: ViewModifier {
    func body(content: Content) -> some View {
        if #available(iOS 15.0, *) {
            content.textSelection(.enabled)
        } else {
            content
        }
    }
}

struct FontModifier: ViewModifier {
    var style: Font.TextStyle = .body
    func body(content: Content) -> some View {
        content
            .font(.system(style, design: .default))
    }
}

struct RowFontMidifer: ViewModifier {
    var size: CGFloat = 18
    func body(content: Content) -> some View {
        content
            .font(.system(size: size, weight: .regular))
            .foregroundColor(Color("c_titleColor"))
    }
}

struct MomentFontMidifer: ViewModifier {
    func body(content: Content) -> some View {
        content
            .font(.system(size: 16, weight: .regular))
            .foregroundColor(.primary)
    }
}

struct CustomFontModifier: ViewModifier {
    var size: CGFloat = 28
    func body(content: Content) -> some View {
        content
            .font(.custom("WorkSans-Bold", size: size))
    }
}

struct HeaderImage: View {
    var image: UIImage
    var body: some View {
        Image(uiImage: image)
            .resizable()
            .aspectRatio(contentMode: .fill)
            .frame(width: 48, height: 48)
            .cornerRadius(10)
            .modifier(Shadow())
    }
}

private var background: some View {
    RoundedRectangle(cornerRadius: 4)
        .foregroundColor(Color("chat_me_background"))
}

extension View {
    var testBorder: some View {
        self.border(Color.green, width: 1)
    }
}

extension View {
    func discoverPosterStyle() -> some View {
        ModifiedContent(content: self, modifier: DiscoverPosterStyle())
    }
}

struct DiscoverPosterStyle: ViewModifier {
    func body(content: Content) -> some View {
        content
            .aspectRatio(0.66, contentMode: .fit)
            .frame(maxWidth: 245)
            .cornerRadius(5)
    }
}

extension UIApplication {
    var key: UIWindow? {
        self.connectedScenes
            .map({$0 as? UIWindowScene})
            .compactMap({$0})
            .first?
            .windows
            .filter({$0.isKeyWindow})
            .first
    }
}


extension UIView {
    func allSubviews() -> [UIView] {
        var subs = self.subviews
        for subview in self.subviews {
            let rec = subview.allSubviews()
            subs.append(contentsOf: rec)
        }
        return subs
    }
}
    

struct TabBarModifier {
    static func showTabBar() {
        UIApplication.shared.key?.allSubviews().forEach({ subView in
            if let tabBar = subView as? UITabBar {
                tabBar.isHidden = false
            }
        })
    }
    
    static func hideTabBar() {
        UIApplication.shared.key?.allSubviews().forEach({ subView in
            if let tabBar = subView as? UITabBar {
                tabBar.isHidden = true
            }
        })
    }
}

struct ShowTabBar: ViewModifier {
    func body(content: Content) -> some View {
        if #available(iOS 16, *) {
            content.toolbar(.hidden, for: .tabBar)
        } else {
            content.padding(.zero).onAppear {
                TabBarModifier.showTabBar()
            }
        }
    }
}

struct HiddenTabBar: ViewModifier {
    func body(content: Content) -> some View {
        if #available(iOS 16, *) {
            content.toolbar(.visible, for: .tabBar)
        } else {
            content.padding(.zero).onAppear {
                TabBarModifier.hideTabBar()
            }
        }
    }
}

struct ActivityModifer: ViewModifier {
    @Binding var loading: Bool
    func body(content: Content) -> some View {
        if loading {
            ZStack {
                content
                ActivityView()
            }
        } else {
            content
        }
    }
}

extension View {
    func showTabBar() -> some View {
        return self.modifier(ShowTabBar())
    }

    func hiddenTabBar() -> some View {
        return self.modifier(HiddenTabBar())
    }
}
